public class Solution {
    public int maxProduct(int[] nums) {
        int len = nums.length;

        int[] f = new int[len];//存较大的值
        int[] g = new int[len];//存较小的值

        f[0] = nums[0];
        g[0] = nums[0];

        //分>0和<0
        for (int i = 1; i < len; i++) {
            f[i] = Math.max(Math.max(f[i - 1] * nums[i], g[i - 1] * nums[i]), nums[i]);
            g[i] = Math.min(Math.min(f[i - 1] * nums[i], g[i - 1] * nums[i]), nums[i]);
        }

        int ret1 = Integer.MIN_VALUE;
        int ret2 = Integer.MIN_VALUE;

        for (int num : f) {
            ret1 = Math.max(ret1, num);
        }

        for (int num : g) {
            ret2 = Math.max(ret2, num);
        }

        return Math.max(ret1, ret2);
    }
}
